<?php

namespace app\api\controller;

use support\Request;
use app\ai\model\User;
use think\facade\Db;

class ConsoleController
{
    
    /**
     * 会员到期脚本
     */
    public function levelEndtime(Request $request){
        $users = User::where('level_endtime','<',time())->select();
        if (empty($users)) {
            return json(['code'=>200,'msg'=>'会员到期检测成功']);
        }
        Db::startTrans();
        try {
            foreach ($users as $user) {
                $user->level = 1;
                $user->save();
            }
            Db::commit();
        } catch (\Throwable $th) {
            Db::rollback();
            return json(['code'=>400,'msg'=>$th->getMessage()]);
        }
        return json(['code'=>200,'msg'=>'会员到期检测成功']);
    }

}
